﻿using System;
using JetBrains.Annotations;
using JetBrains.DocumentModel;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Daemon.Impl;
using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using MugenMvvmToolkit.ReSharper.Highlighting;

[assembly:
    RegisterConfigurableSeverity(UnusedModelPropertyAttributeHighlighting.Id, null, HighlightingGroupIds.CodeRedundancy,
        "Unused ModelPropertyAttribute", "Unused ModelPropertyAttribute", Severity.WARNING, false)]

namespace MugenMvvmToolkit.ReSharper.Highlighting
{
    [ConfigurableSeverityHighlighting(Id, CSharpLanguage.Name,
        OverlapResolve = OverlapResolveKind.DEADCODE, AttributeId = HighlightingAttributeIds.DEADCODE_ATTRIBUTE)]
    public class UnusedModelPropertyAttributeHighlighting : IHighlightingWithRange
    {
        #region Fields

        public const string Id = "UnusedModelPropertyAttributeHighlighting";
        private readonly IAttribute _attribute;
        private readonly IPropertyDeclaration _propertyDeclaration;

        #endregion

        #region Constructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="UnusedModelPropertyAttributeHighlighting" /> class.
        /// </summary>
        public UnusedModelPropertyAttributeHighlighting([NotNull] IPropertyDeclaration propertyDeclaration,
            [NotNull] IAttribute attribute)
        {
            if (propertyDeclaration == null) throw new ArgumentNullException("propertyDeclaration");
            if (attribute == null) throw new ArgumentNullException("attribute");
            _propertyDeclaration = propertyDeclaration;
            _attribute = attribute;
        }

        #endregion

        #region Implementation of IHighlighting

        /// <summary>
        ///     Returns true if data (PSI, text ranges) associated with highlighting is valid
        /// </summary>
        public bool IsValid()
        {
            return _attribute.IsValid();
        }

        /// <summary>
        ///     Message for this highlighting to show in tooltip and in status bar (if
        ///     <see cref="P:JetBrains.ReSharper.Daemon.HighlightingAttributeBase.ShowToolTipInStatusBar" /> is <c>true</c>)
        ///     To override the default mechanism of tooltip, mark the implementation class with
        ///     <see cref="T:JetBrains.ReSharper.Daemon.DaemonTooltipProviderAttribute" /> attribute, and then this property will
        ///     not be called
        /// </summary>
        public string ToolTip
        {
            get { return UiResources.UnusedModelPropertyError; }
        }

        /// <summary>
        ///     Message for this highlighting to show in tooltip and in status bar (if
        ///     <see cref="P:JetBrains.ReSharper.Daemon.HighlightingAttributeBase.ShowToolTipInStatusBar" /> is <c>true</c>)
        /// </summary>
        public string ErrorStripeToolTip
        {
            get { return ToolTip; }
        }

        /// <summary>
        ///     Specifies the offset from the Range.StartOffset to set the cursor to when navigating
        ///     to this highlighting. Usually returns <c>0</c>
        /// </summary>
        public int NavigationOffsetPatch
        {
            get { return 0; }
        }

        public DocumentRange CalculateRange()
        {
            return _attribute.GetHighlightingRange();
        }

        #endregion

        #region Properties

        public IPropertyDeclaration PropertyDeclaration
        {
            get { return _propertyDeclaration; }
        }

        public IAttribute Attribute
        {
            get { return _attribute; }
        }

        #endregion
    }
}